package web

import (
	"context"
	"gitee.com/alex-pistachio/axel/utils"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/hertz-contrib/requestid"
	"reflect"
)

func NewRestHandler(o IController) app.HandlerFunc {
	t := reflect.TypeOf(o).Elem()

	return func(c context.Context, ctx *app.RequestContext) {
		obj := reflect.New(t).Interface().(IController)
		if err := obj.Prepare(c, ctx, obj); err == nil {
			obj.Execute()
		}

		obj.Finish()
	}
}

func NewRequestIdWithNanoid() app.HandlerFunc {
	return requestid.New(requestid.WithGenerator(func(ctx context.Context, c *app.RequestContext) string {
		return utils.NanoId()
	}))
}

func NewRequestIdWithUUID() app.HandlerFunc {
	return requestid.New()
}
